<template>
	<view class="register">
		<view class="title">
			<view class="title-tips">新用户注册</view>
			<view class="goto" @click="$util.redirectTo('/pages/login/login')">已有账号，去登录</view>
		</view>
		<view class="input">
			<input placeholder="请输入手机号" v-model="from.username"/>
		</view>
		<view class="input input-code">
			<input placeholder="请输入验证码" v-model="from.verification_code"/>
			<text class="txt" @click="getcode" v-if="!time_num">获取验证码</text>
			<text class="txt code" v-else>{{time_num}}s</text>
		</view>
		<view class="input ">
			<input placeholder="设置登录密码（需包含数字与字母）" class="input-1" v-model="from.password"/>
		</view>
		<view class="input">
			<text v-if="judge">{{from.code}}</text>
			<input v-else placeholder="输入邀请码（选填）" v-model="from.code"/>
		</view>
		<u-button class="primary" @click="btn" ripple-bg-color="rgba(0,0,0,.4)" :ripple="true">注册</u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				time_num:0,
				from:{
					username:'',
					verification_code:'',
					password:'',
					code:'',
				},
				judge:false
			}
		},
		onLoad(option) {
			if(option.code) {
				this.from.code = option.code
				this.judge = true;
			}
			if(uni.getStorageSync('code')) {
				this.from.code = uni.getStorageSync('code')
				this.judge = true;
			} 
		},
		methods:{
			getcode(){
				this.$http.asyncSendrequest({
					url:'/login/sms_verification_code',
					data:{
						sms_type:2,
						mobile:this.from.username,
						type:'register'
					},
					success:res=>{
						if(res.code == 1){
							this.time_num = 60;
							const countdown = setInterval(()=>{
								this.time_num -= 1;
								if(!this.time_num) clearTimeout(countdown);
							},1000)
						} 
						this.$util.taost({
							title:res.msg
						})
					}
				})
			},
			btn(){
				var txt = ''
				if(!this.from.username)	txt = '请输入手机号'
				if(!this.from.verification_code)	txt = '请输入验证码'
				if(!this.from.password)	txt = '请输入密码'
				if(txt){
					this.$util.taost({
						title:txt
					})
					return false;
				}
				this.$http.asyncSendrequest({
					url:'/login/register',
					data:this.from,
					success:res=>{
						this.$util.taost({
							title:res.msg,
							callback:v=>{
								if(res.code == '0'){
									this.$util.redirectTo('/pages/login/login')
								}
							}
						})

					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.register {
		width: 100%;
		min-height: 100vh;
		padding-top: 178rpx;

		.title {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin: 0 38rpx 60rpx;
			color: var(--title-project);

			.title-tips {
				font-size: 50rpx;
				font-family: PingFang SC;
				font-weight: bold;
			}

			.goto {
				font-size: 26rpx;
				font-family: PingFang SC;
				font-weight: bold;
			}
		}

		.input {
			margin: 0 40rpx 24rpx;
			padding: 0 35rpx;
			box-sizing: border-box;
			height: 100rpx;
			border: 1rpx solid #292929;
			border-radius: 10rpx;
			display: flex;
			align-items: center;
			position: relative;

			input{
				width: calc(100% - 200rpx);
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
			}
			.input-1{
				width: 80%;
			}
		}

		.input-code {
			justify-content: space-between;
			input{
				width: calc(100% - 200rpx);
			}
			.txt {
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #000000;
				text-align: right;
			}

			.code {
				color: rgba(0, 0, 0, .4);
				
			}
		}
		.primary{
			height: 100rpx;
			background: #292929;
			border-radius: 100rpx;
			margin: 64rpx 40rpx 0;
			color: #fff;
		}
	}
</style>